﻿using DTO;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAO
{
    public class TrongTai_DAO
    {
        public static DataTable SelectAll()
        {
            string sql = "select * from TRONGTAI";
            return DataAccess.executeQuery(sql);
        }
        
        public static TrongTai_DTO SelectByID(int id)
        {
            string sql = "select * from TRONGTAI where MaTrongTai = " + id;
            DataTable dt = DataAccess.executeQuery(sql);
            TrongTai_DTO dto = new TrongTai_DTO();
            dto.MaTrongTai = Convert.ToInt32(dt.Rows[0]["MaTrongTai"]);
            dto.TenTrongTai = Convert.ToString(dt.Rows[0]["TenTrongTai"]);
            dto.QuocTich = Convert.ToString(dt.Rows[0]["QuocTich"]);
            dto.NgaySinh = Convert.ToDateTime(dt.Rows[0]["NgaySinh"]);
            dto.Chinh = Convert.ToBoolean(dt.Rows[0]["Chinh"]);
            dto.Bien = Convert.ToBoolean(dt.Rows[0]["Bien"]);
            
            return dto;
        }
        public static DataTable SelectByTen(string ten)
        {
            string sql = "select * from TRONGTAI where TenTrongTai = '" + ten + "'";
            DataTable dt = DataAccess.executeQuery(sql);
            return dt;
        }
        public static void Insert(TrongTai_DTO dto)
        {
            string sql = "Insert INTO TRONGTAI (TenTrongTai, QuocTich, NgaySinh, Chinh, Bien)" +
                            "VALUES ('" + dto.TenTrongTai +
                                     "','" + dto.QuocTich +
                                     "','" + dto.NgaySinh +
                                     "'," + dto.Chinh +
                                     "," + dto.Bien + ")";

            DataAccess.executeNonQuery(sql);
        }
        public static void Update(TrongTai_DTO dto)
        {
            string sql = "Update TRONGTAI set TenTrongTai=('" + dto.TenTrongTai +
                                            "'),QuocTich=('" + dto.QuocTich +
                                            "'),NgaySinh=('" + dto.NgaySinh +
                                            "'),Chinh=(" + dto.Chinh +
                                            "),Bien=(" + dto.Bien + ") where MaTrongTai = " + dto.MaTrongTai + "";

            DataAccess.executeNonQuery(sql);
        }
        public static void Delete(int ma)
        {
            string sql = "Delete from TRONGTAI where MaTrongTai = " + ma + "";
            DataAccess.executeNonQuery(sql);
        }
        static public DataTable SelectLikeTen(string ten)
        {
            string sql = "select * from TRONGTAI where TenTrongTai like '%" + ten + "%'";
            return DataAccess.executeQuery(sql);
        }
    }
}
